package com.etl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.etl.entity.EtlUser;
import com.etl.model.rev.EtlUserRev;
import com.etl.model.rev.EtlUserSaveRev;
import com.etl.service.IEtlUserService;
import com.etl.mapper.EtlUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class EtlUserServiceImpl implements IEtlUserService {

    @Resource
    private EtlUserMapper etlUserMapper;

    @Override
    public PageInfo<EtlUser> getEtlUser(EtlUserRev etlUserRev) {
        PageHelper.startPage(etlUserRev.getPages(), etlUserRev.getSizes());
        QueryWrapper<EtlUser> queryWrapper = new QueryWrapper<>();

//        queryWrapper.like("source_table_name", etlTaskListRev.getSrctable().trim());
        queryWrapper.orderByDesc("update_time");
        List<EtlUser> etlMainTasks = etlUserMapper.selectList(queryWrapper);
        PageInfo<EtlUser> pageInfoDemo = new PageInfo<>(etlMainTasks);
        return pageInfoDemo;
    }

    @Override
    public void saveOrUpdateEtlUser(EtlUserSaveRev etlUserSaveRev){

        EtlUser etlUser = new EtlUser();
        BeanUtils.copyProperties(etlUserSaveRev,etlUser);
        //存在，修改
        if(etlUser.getId() != null){
            etlUser.setUpdateTime(new Date());
            etlUserMapper.updateById(etlUser);
        }else{
            etlUser.setCreateTime(new Date());
            etlUser.setUpdateTime(new Date());
            etlUserMapper.insert(etlUser);
        }


    }

    @Override
    public List<EtlUser> getEtlUser(EtlUser etlUser){
        QueryWrapper<EtlUser> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(etlUser.getAccount()) ){
            queryWrapper.eq("account",etlUser.getAccount());
        }
        if(StringUtils.isNotEmpty(etlUser.getPassword()) ){
            queryWrapper.eq("password",etlUser.getPassword());
        }

        return etlUserMapper.selectList(queryWrapper);
    }


}
